package com.label.grade.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.label.grade.entity.Chart.GroupedColumn;
import com.label.grade.entity.Do.*;
import com.label.grade.entity.Vo.*;
import com.label.grade.mapper.CetInfoMapper;
import com.label.grade.mapper.CourseGradeMapper;
import com.label.grade.mapper.UndergraGradeMapper;
import com.label.grade.mapper.UndergraInfoMapper;
import com.label.grade.service.UndergraGradeService;
import com.label.grade.utils.ExamSemesterCalculatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Administrator
 * @description 针对表【UNDERGRA_GRADE】的数据库操作Service实现
 * @createDate 2023-07-07 09:28:11
 */
@Service
public class UndergraGradeServiceImpl extends ServiceImpl<UndergraGradeMapper, UndergraGrade>
        implements UndergraGradeService {
    @Autowired
    UndergraGradeMapper undergraGradeMapper;
    @Autowired
    UndergraInfoMapper undergraInfoMapper;
    @Autowired
    CourseGradeMapper courseGradeMapper;
    @Autowired
    CetInfoMapper cetInfoMapper;

    /**
    * @Description: 计算Gpa和学分
    * @Param:
    * @return:
    * @Author: i++
    * @Date: 2023/7/8
    */
    public void caculateGpaAndCredit(){
        //查找本科生信息
        UndergraInfo undergraInfoDo = new UndergraInfo();
        List<UndergraInfo> undergraInfoList = new ArrayList<>();
        undergraInfoList = undergraInfoDo.selectAll();
        int n = 0;
        QueryWrapper<CourseGrade> courseGradeDoQueryWrapper = new QueryWrapper<>();
        courseGradeDoQueryWrapper.orderByDesc("STU_NUMBER");
        CourseGrade courseGradeDo = new CourseGrade();
        List<CourseGrade> courseGradeDos = courseGradeDo.selectList(courseGradeDoQueryWrapper);
        UndergraGrade undergraGradeDo = new UndergraGrade();//学生成绩，存储每一学期的gpa和学分
        undergraGradeDo.setStuName(courseGradeDos.get(0).getStuName());
        undergraGradeDo.setStuNumber(courseGradeDos.get(0).getStuNumber());
        for (CourseGrade item:courseGradeDos
        ) {
            //如果此记录和上一条记录不是同一人则重置undergraGradeDo
            if (!item.getStuNumber().equals(undergraGradeDo.getStuNumber())) {
                n++;
                //根据算法计算绩点
                //由于学分可以每学期单独计算所以无需处理，但是绩点是累计的过程
                undergraGradeDo.setGpa12(undergraGradeDo.getGpa11()+undergraGradeDo.getGpa12());
                undergraGradeDo.setGpa21(undergraGradeDo.getGpa12()+undergraGradeDo.getGpa21());
                undergraGradeDo.setGpa22(undergraGradeDo.getGpa21()+undergraGradeDo.getGpa22());
                undergraGradeDo.setGpa31(undergraGradeDo.getGpa22()+undergraGradeDo.getGpa31());
                undergraGradeDo.setGpa32(undergraGradeDo.getGpa31()+undergraGradeDo.getGpa32());
                undergraGradeDo.setGpa41(undergraGradeDo.getGpa32()+undergraGradeDo.getGpa41());
                undergraGradeDo.setGpa42(undergraGradeDo.getGpa41()+undergraGradeDo.getGpa42());
                Double credit11 = undergraGradeDo.getCredit11();
                Double credit12 = undergraGradeDo.getCredit12() + credit11;
                Double credit21 = undergraGradeDo.getCredit21() + credit12;
                Double credit22 = undergraGradeDo.getCredit22() + credit21;
                Double credit31 = undergraGradeDo.getCredit31() + credit22;
                Double credit32 = undergraGradeDo.getCredit32() + credit31;
                Double credit41 = undergraGradeDo.getCredit41() + credit32;
                Double credit42 = undergraGradeDo.getCredit42() + credit41;
                undergraGradeDo.setGpa(undergraGradeDo.getCredit() != 0 ? undergraGradeDo.getGpa() / undergraGradeDo.getCredit() : 0);
                undergraGradeDo.setGpa11(undergraGradeDo.getCredit11() != 0 ? undergraGradeDo.getGpa11() / credit11 : 0);
                undergraGradeDo.setGpa12(undergraGradeDo.getCredit12() != 0 ? undergraGradeDo.getGpa12() / credit12 : 0);
                undergraGradeDo.setGpa21(undergraGradeDo.getCredit21() != 0 ? undergraGradeDo.getGpa21() / credit21 : 0);
                undergraGradeDo.setGpa22(undergraGradeDo.getCredit22() != 0 ? undergraGradeDo.getGpa22() / credit22 : 0);
                undergraGradeDo.setGpa31(undergraGradeDo.getCredit31() != 0 ? undergraGradeDo.getGpa31() / credit31 : 0);
                undergraGradeDo.setGpa32(undergraGradeDo.getCredit32() != 0 ? undergraGradeDo.getGpa32() / credit32 : 0);
                undergraGradeDo.setGpa41(undergraGradeDo.getCredit41() != 0 ? undergraGradeDo.getGpa41() / credit41 : 0);
                undergraGradeDo.setGpa42(undergraGradeDo.getCredit42() != 0 ? undergraGradeDo.getGpa42() / credit42 : 0);
                //将上一条记录插入或者更新
                System.out.println(undergraGradeDo);
                QueryWrapper<UndergraGrade> undergraGradeQueryWrapper = new QueryWrapper<>();
                undergraGradeQueryWrapper.eq("STU_NUMBER",undergraGradeDo.getStuNumber());
                UndergraGrade undergraGradeTemp = undergraGradeDo.selectOne(undergraGradeQueryWrapper);
                //从学生信息表中查找此学生班级专业并赋值
                for(UndergraInfo undergraInfo : undergraInfoList){
                    if (undergraInfo.getStuNumber().equals(undergraGradeDo.getStuNumber())){
                        undergraGradeDo.setClassName(undergraInfo.getClassName());
                        undergraGradeDo.setMajorName(undergraInfo.getMajor());
                    }
                }
                if (undergraGradeTemp != null){
                    //更新
                    undergraGradeMapper.update(undergraGradeDo,undergraGradeQueryWrapper);
                }else {
                    //插入
                    undergraGradeDo.insert();
                }
                //重置
                undergraGradeDo = new UndergraGrade();
                undergraGradeDo.setStuName(item.getStuName());
                undergraGradeDo.setStuNumber(item.getStuNumber());
            }
            //计算学分绩点
            //判断是哪个学期
            String enrollYear = "20" + item.getStuNumber().substring(0, 2);//获取入学年份
            Map<String, Integer> map = ExamSemesterCalculatorUtil.main(enrollYear, item.getExamDate());
            //计算当前课程学分
            Double myCredit = item.getCredit();
            if (item.getScore() < 60) {
                myCredit = 0D;
            }
            //计算每个学期分数
            switch (map.get("academicYear")) {
                case 1:
                    if (map.get("semester") == 1) {//如果是第一学期
                        undergraGradeDo.setCredit11(undergraGradeDo.getCredit11() + myCredit);
                        undergraGradeDo.setGpa11(undergraGradeDo.getGpa11() + (item.getGpaScore() * item.getCredit()));
                    } else {
                        undergraGradeDo.setCredit12(undergraGradeDo.getCredit12() + myCredit);
                        undergraGradeDo.setGpa12(undergraGradeDo.getGpa12() + (item.getGpaScore() * item.getCredit()));
                    }
                    break;
                case 2:
                    if (map.get("semester") == 1) {//如果是第一学期
                        undergraGradeDo.setCredit21(undergraGradeDo.getCredit21() + myCredit);
                        undergraGradeDo.setGpa21(undergraGradeDo.getGpa21() + (item.getGpaScore() * item.getCredit()));
                    } else {
                        undergraGradeDo.setCredit22(undergraGradeDo.getCredit22() + myCredit);
                        undergraGradeDo.setGpa22(undergraGradeDo.getGpa22() + (item.getGpaScore() * item.getCredit()));
                    }
                    break;
                case 3:
                    if (map.get("semester") == 1) {//如果是第一学期
                        undergraGradeDo.setCredit31(undergraGradeDo.getCredit31() + myCredit);
                        undergraGradeDo.setGpa31(undergraGradeDo.getGpa31() + (item.getGpaScore() * item.getCredit()));
                    } else {
                        undergraGradeDo.setCredit32(undergraGradeDo.getCredit32() + myCredit);
                        undergraGradeDo.setGpa32(undergraGradeDo.getGpa32() + (item.getGpaScore() * item.getCredit()));
                    }
                    break;
                case 4:
                    if (map.get("semester") == 1) {//如果是第一学期
                        undergraGradeDo.setCredit41(undergraGradeDo.getCredit41() + myCredit);
                        undergraGradeDo.setGpa41(undergraGradeDo.getGpa41() + (item.getGpaScore() * item.getCredit()));
                    } else {
                        undergraGradeDo.setCredit42(undergraGradeDo.getCredit42() + myCredit);
                        undergraGradeDo.setGpa42(undergraGradeDo.getGpa42() + (item.getGpaScore() * item.getCredit()));
                    }
                    break;
                default:
                    break;
            }
            //计算总分
            undergraGradeDo.setCredit(undergraGradeDo.getCredit() + myCredit);
            undergraGradeDo.setGpa(undergraGradeDo.getGpa() + (item.getGpaScore() * item.getCredit()));
        }
        System.out.println("--------------------------------"+n);
    }

    /**
    * @Description: 学生：成绩汇总界面
    * @Param: stuNumber
    * @return: gradeSummaryVoList
    * @Author: i++
    * @Date: 2023/7/8
    */
    public GradeSummaryVo caculateGradeSummary(String stuNumber){
        GradeSummaryVo gradeSummaryVo = new GradeSummaryVo();
        //课程成绩
        GradeSummaryCourseGradesVo gradeSummaryCourseGradesVo = undergraGradeMapper.selectCourseGrades(stuNumber);
        //二课成绩
        ExtracurricularScoreSummary extracurricularScoreSummary = new ExtracurricularScoreSummary();
        QueryWrapper<ExtracurricularScoreSummary> extracurricularScoreSummaryQueryWrapper = new QueryWrapper<>();
        extracurricularScoreSummaryQueryWrapper.eq("STUDENT_CODE", stuNumber);
        extracurricularScoreSummary = extracurricularScoreSummary.selectOne(extracurricularScoreSummaryQueryWrapper);
        GradeSummarySecondaryGradesVo gradeSummarySecondaryGradesVo = new GradeSummarySecondaryGradesVo();
        if (extracurricularScoreSummary != null){
            gradeSummarySecondaryGradesVo = new GradeSummarySecondaryGradesVo(extracurricularScoreSummary.getNationalLevel(),extracurricularScoreSummary.getProvincialLevel(),extracurricularScoreSummary.getUniversityLevel(),extracurricularScoreSummary.getCollegeLevel(),extracurricularScoreSummary.getOverallGrade());
        }

        //四六级
        GradeSummaryCetGradesVo gradeSummaryCetGradesVo = cetInfoMapper.selectCetGrades(stuNumber);
        if(gradeSummaryCetGradesVo == null){
            gradeSummaryCetGradesVo = new GradeSummaryCetGradesVo();
        }
        //体测成绩
        PhysicalFitnessInfo physicalFitnessInfo = new PhysicalFitnessInfo();
        QueryWrapper<PhysicalFitnessInfo> physicalFitnessInfoQueryWrapper = new QueryWrapper<>();
        physicalFitnessInfoQueryWrapper.eq("STUDENT_CODE", stuNumber);
        physicalFitnessInfo = physicalFitnessInfo.selectOne(physicalFitnessInfoQueryWrapper);
        GradeSummarySportsGrades gradeSummarySportsGrades = new GradeSummarySportsGrades();
        if (physicalFitnessInfo != null){
            gradeSummarySportsGrades = new GradeSummarySportsGrades(physicalFitnessInfo.getYear1Grade(),physicalFitnessInfo.getYear2Grade(),physicalFitnessInfo.getYear3Grade(),physicalFitnessInfo.getYear4Grade());
        }
        //赋值
        gradeSummaryVo.setCourseGrade(gradeSummaryCourseGradesVo);
        gradeSummaryVo.setSecondaryGrades(gradeSummarySecondaryGradesVo);
        gradeSummaryVo.setCetGrades(gradeSummaryCetGradesVo);
        gradeSummaryVo.setSportsGrades(gradeSummarySportsGrades);
        return gradeSummaryVo;
    }

    /**
    * @Description: 一课学情分析页面数据
    * @Param:
    * @return:
    * @Author: i++
    * @Date: 2023/7/9
    */
    public GradeLearnAnalysisVo selectGradeLearnAnalysis(String stuNumber){
        GradeLearnAnalysisVo gradeLearnAnalysisVo = new GradeLearnAnalysisVo();
        //学生成绩基本信息
        GradeSummaryCourseGradesVo gradeSummaryCourseGradesVo = undergraGradeMapper.selectCourseGrades(stuNumber);
        if (gradeSummaryCourseGradesVo != null){
            gradeLearnAnalysisVo.setGradeBaseInfo(gradeSummaryCourseGradesVo);
        }else {
            gradeLearnAnalysisVo.setGradeBaseInfo(new GradeSummaryCourseGradesVo());
        }
        //绩点趋势图
        GpaCompareVo gpaCompare = undergraGradeMapper.selectGpaCompare(stuNumber);
        if(gpaCompare != null){
            gradeLearnAnalysisVo.setGpaCompare(gpaCompare);
        }else {
            gradeLearnAnalysisVo.setGpaCompare(new GpaCompareVo());
        }
        //挂科科目
        List<FailedSubjectVo> failedSubjectVoList  = undergraGradeMapper.selectFailedSubject(stuNumber);
        if (failedSubjectVoList.size() != 0){
            String enrollYear = "20" + failedSubjectVoList.get(0).getStuNumber().substring(0, 2);//获取入学年份
            for (FailedSubjectVo failedSubjectVo:failedSubjectVoList) {
                Map<String,Integer> map = ExamSemesterCalculatorUtil.main(enrollYear, failedSubjectVo.getExamDate());
                failedSubjectVo.setSemester("第"+map.get("academicYear")+"学年"+"第"+map.get("semester")+"学期");
            }
            gradeLearnAnalysisVo.setFailedSubjectList(failedSubjectVoList);
        }else {
            gradeLearnAnalysisVo.setFailedSubjectList(new ArrayList<>());
        }
        return gradeLearnAnalysisVo;
    }

    /**
    * @Description: 导员、班导师查询本学生的全部成绩
    * @Param:
    * @return:
    * @Author: i++
    * @Date: 2023/7/9
    */
    public List<GradeListVo> selectGradeList(String teacherNumber){
        return undergraGradeMapper.selectGradeList(teacherNumber);
    }

    /**
    * @Description: 育人团队负责人查询本学生的全部成绩
    * @Param:
    * @return:
    * @Author: i++
    * @Date: 2023/7/9
    */
    public List<GradeListVo> selectGradeForLeaderList(String teacherNumber){
        EducationTeamMemeber educationTeamMemeber = new EducationTeamMemeber();
        QueryWrapper<EducationTeamMemeber> educationTeamMemeberQueryWrapper = new QueryWrapper<>();
        educationTeamMemeberQueryWrapper.eq("leader_number", teacherNumber);
        List<EducationTeamMemeber> educationTeamMemeberList = educationTeamMemeber.selectList(educationTeamMemeberQueryWrapper);
        List<String> teacherList = new ArrayList<>();
        for (EducationTeamMemeber e:educationTeamMemeberList
             ) {
            if (e.getInstructorNumber() != null){
                teacherList.add(e.getInstructorNumber());
            }
        }
        return undergraGradeMapper.selectGradeForLeaderList(teacherList);
    }

    /**
    * @Description: 选择个人详情成绩
    * @Param:
    * @return:
    * @Author: i++
    * @Date: 2023/7/10
    */
    public List<DetailScoreVo> selectDetailScore(String stuNumber){
        DetailScoreVo detailScoreVo = new DetailScoreVo();
        //查询课程信息并将考试日期改成学年学期以及学分修改
        QueryWrapper<CourseGrade> courseGradeDoQueryWrapper = new QueryWrapper<>();
        courseGradeDoQueryWrapper.eq("STU_NUMBER", stuNumber);
        CourseGrade courseGradeDo = new CourseGrade();
        List<CourseGrade> courseGradeList = courseGradeDo.selectList(courseGradeDoQueryWrapper);
        if (courseGradeList.size() != 0){
            for (CourseGrade courseGrade:courseGradeList) {
                if(courseGrade.getScore() < 60){
                    courseGrade.setCredit(0D);
                }
                String enrollYear = "20" + courseGrade.getStuNumber().substring(0,2);
                Map<String,Integer> map = ExamSemesterCalculatorUtil.main(enrollYear,courseGrade.getExamDate());
                courseGrade.setExamDate("第"+map.get("academicYear")+"学年"+"第"+map.get("semester")+"学期");
            }
        }
        List<DetailScoreVo> detailScoreVoList = new ArrayList<>();
        DetailScoreVo detailScoreVo11 = new DetailScoreVo();
        DetailScoreVo detailScoreVo12 = new DetailScoreVo();
        DetailScoreVo detailScoreVo21 = new DetailScoreVo();
        DetailScoreVo detailScoreVo22 = new DetailScoreVo();
        DetailScoreVo detailScoreVo31 = new DetailScoreVo();
        DetailScoreVo detailScoreVo32 = new DetailScoreVo();
        DetailScoreVo detailScoreVo41 = new DetailScoreVo();
        DetailScoreVo detailScoreVo42 = new DetailScoreVo();
        List<CourseGrade> courseGradeDoList11 = new ArrayList<>();
        List<CourseGrade> courseGradeDoList12 = new ArrayList<>();
        List<CourseGrade> courseGradeDoList21 = new ArrayList<>();
        List<CourseGrade> courseGradeDoList22 = new ArrayList<>();
        List<CourseGrade> courseGradeDoList31 = new ArrayList<>();
        List<CourseGrade> courseGradeDoList32 = new ArrayList<>();
        List<CourseGrade> courseGradeDoList41 = new ArrayList<>();
        List<CourseGrade> courseGradeDoList42 = new ArrayList<>();
        for (CourseGrade courseGrade:courseGradeList) {
            if ("第1学年第1学期".equals(courseGrade.getExamDate())){
                courseGradeDoList11.add(courseGrade);
                //设置获得学分和绩点
                detailScoreVo11.setCredit(detailScoreVo11.getCredit() + courseGrade.getCredit());
                detailScoreVo11.setGpa(detailScoreVo11.getGpa() + courseGrade.getGpaScore());
            }else if("第1学年第2学期".equals(courseGrade.getExamDate())){
                courseGradeDoList12.add(courseGrade);
                //设置获得学分和绩点
                detailScoreVo12.setCredit(detailScoreVo12.getCredit() + courseGrade.getCredit());
                detailScoreVo12.setGpa(detailScoreVo12.getGpa() + courseGrade.getGpaScore());
            }else if("第2学年第1学期".equals(courseGrade.getExamDate())){
                courseGradeDoList21.add(courseGrade);
                //设置获得学分和绩点
                detailScoreVo21.setCredit(detailScoreVo21.getCredit() + courseGrade.getCredit());
                detailScoreVo21.setGpa(detailScoreVo21.getGpa() + courseGrade.getGpaScore());
            }else if("第2学年第2学期".equals(courseGrade.getExamDate())){
                courseGradeDoList22.add(courseGrade);
                //设置获得学分和绩点
                detailScoreVo22.setCredit(detailScoreVo22.getCredit() + courseGrade.getCredit());
                detailScoreVo22.setGpa(detailScoreVo22.getGpa() + courseGrade.getGpaScore());
            }else if("第3学年第1学期".equals(courseGrade.getExamDate())){
                courseGradeDoList31.add(courseGrade);
                //设置获得学分和绩点
                detailScoreVo31.setCredit(detailScoreVo31.getCredit() + courseGrade.getCredit());
                detailScoreVo31.setGpa(detailScoreVo31.getGpa() + courseGrade.getGpaScore());
            }else if("第3学年第2学期".equals(courseGrade.getExamDate())){
                courseGradeDoList32.add(courseGrade);
                //设置获得学分和绩点
                detailScoreVo32.setCredit(detailScoreVo32.getCredit() + courseGrade.getCredit());
                detailScoreVo32.setGpa(detailScoreVo32.getGpa() + courseGrade.getGpaScore());
            }else if("第4学年第1学期".equals(courseGrade.getExamDate())){
                courseGradeDoList41.add(courseGrade);
                //设置获得学分和绩点
                detailScoreVo41.setCredit(detailScoreVo41.getCredit() + courseGrade.getCredit());
                detailScoreVo41.setGpa(detailScoreVo41.getGpa() + courseGrade.getGpaScore());
            }else if("第4学年第2学期".equals(courseGrade.getExamDate())){
                courseGradeDoList42.add(courseGrade);
                //设置获得学分和绩点
                detailScoreVo42.setCredit(detailScoreVo42.getCredit() + courseGrade.getCredit());
                detailScoreVo42.setGpa(detailScoreVo42.getGpa() + courseGrade.getGpaScore());
            }
        }
        //设置每学期课程数量
        detailScoreVo11.setCourseNumber(courseGradeDoList11.size());
        detailScoreVo12.setCourseNumber(courseGradeDoList12.size());
        detailScoreVo21.setCourseNumber(courseGradeDoList21.size());
        detailScoreVo22.setCourseNumber(courseGradeDoList22.size());
        detailScoreVo31.setCourseNumber(courseGradeDoList31.size());
        detailScoreVo32.setCourseNumber(courseGradeDoList32.size());
        detailScoreVo41.setCourseNumber(courseGradeDoList41.size());
        detailScoreVo42.setCourseNumber(courseGradeDoList42.size());
        detailScoreVo11.setCourseGradeList(courseGradeDoList11);
        detailScoreVo12.setCourseGradeList(courseGradeDoList12);
        detailScoreVo21.setCourseGradeList(courseGradeDoList21);
        detailScoreVo22.setCourseGradeList(courseGradeDoList22);
        detailScoreVo31.setCourseGradeList(courseGradeDoList31);
        detailScoreVo32.setCourseGradeList(courseGradeDoList32);
        detailScoreVo41.setCourseGradeList(courseGradeDoList41);
        detailScoreVo42.setCourseGradeList(courseGradeDoList42);
        detailScoreVoList.add(detailScoreVo11);
        detailScoreVoList.add(detailScoreVo12);
        detailScoreVoList.add(detailScoreVo21);
        detailScoreVoList.add(detailScoreVo22);
        detailScoreVoList.add(detailScoreVo31);
        detailScoreVoList.add(detailScoreVo32);
        detailScoreVoList.add(detailScoreVo41);
        detailScoreVoList.add(detailScoreVo42);
        // 处理未来课程
        UndergraInfo undergraInfo = new UndergraInfo();
        QueryWrapper<UndergraInfo> undergraInfoQueryWrapper = new QueryWrapper<>();
        undergraInfoQueryWrapper.eq("stu_number",stuNumber);
        undergraInfo = undergraInfo.selectOne(undergraInfoQueryWrapper);
        if(undergraInfo != null){
            List<AcademicProgram> academicProgramList = new ArrayList<>();
            QueryWrapper<AcademicProgram> academicProgramQueryWrapper = new QueryWrapper<>();
            academicProgramQueryWrapper.eq("grade", undergraInfo.getEnrollmentYear()).eq("major",undergraInfo.getMajor());
            academicProgramList = new AcademicProgram().selectList(academicProgramQueryWrapper);
            for (int i = 0; i < academicProgramList.size(); i++) {
                academicProgramList.get(i).setSemester();
            }
            //判断学期
            List<DetailScoreVo> detailScoreVoListTemp = new ArrayList<>();
            for (int i = 0; i < detailScoreVoList.size(); i++) {
                if (detailScoreVoList.get(i).getCourseNumber() == 0){
                    DetailScoreVo detailScoreVoTemp = new DetailScoreVo();
                    List<CourseGrade> courseGradeListTemp = new ArrayList<>();
                    for (int j = 0; j < academicProgramList.size(); j++) {
                        if (i == academicProgramList.get(j).getSemesterInt()){
                            CourseGrade courseGrade = new CourseGrade(undergraInfo.getStuNumber(),undergraInfo.getStuName(),academicProgramList.get(j).getCourseName(),academicProgramList.get(j).getSemester(),academicProgramList.get(j).getCredit());
                            courseGradeListTemp.add(courseGrade);
                        }
                    }
                    detailScoreVoTemp.setCourseNumber(courseGradeListTemp.size());
                    detailScoreVoTemp.setCourseGradeList(courseGradeListTemp);
                    detailScoreVoListTemp.add(detailScoreVoTemp);
                }else {
                    detailScoreVoListTemp.add(detailScoreVoList.get(i));
                }
            }
            return detailScoreVoListTemp;
        }else return detailScoreVoList;
    }

    /**
    * @Description: 班导师其他成绩列表
    * @Param:
    * @return:
    * @Author: i++
    * @Date: 2023/7/12
    */
    public List<OtherGradeVo> selectOtherGrade(String teacherNumber){
        List<OtherGradeVo> otherGradeVoList = new ArrayList<>();
        otherGradeVoList = undergraGradeMapper.selectOtherGrade(teacherNumber);
        return otherGradeVoList;
    }

    /**
    * @Description: 育人团队其他成绩列表
    * @Param:
    * @return:
    * @Author: i++
    * @Date: 2023/7/12
    */
    public List<OtherGradeVo> selectOtherGradeForLeader(String teacherNumber){
        List<OtherGradeVo> otherGradeVoList = new ArrayList<>();
        EducationTeamMemeber educationTeamMemeber = new EducationTeamMemeber();
        QueryWrapper<EducationTeamMemeber> educationTeamMemeberQueryWrapper = new QueryWrapper<>();
        educationTeamMemeberQueryWrapper.eq("leader_number", teacherNumber);
        List<EducationTeamMemeber> educationTeamMemeberList = educationTeamMemeber.selectList(educationTeamMemeberQueryWrapper);
        List<String> teacherList = new ArrayList<>();
        for (EducationTeamMemeber e:educationTeamMemeberList
        ) {
            if (e.getInstructorNumber() != null){
                teacherList.add(e.getInstructorNumber());
            }
        }
        return undergraGradeMapper.selectOtherGradeForLeader(teacherList);
    }

    /**
    * @Description: 获取课程成绩单（辅导员）
    * @Param:
    * @return:
    * @Author: i++
    * @Date: 2023/7/12
    */
    public CourseScoreDetailVo selectCourseScoreDetail(String stuNumber){
        CourseScoreDetailVo courseScoreDetailVo = undergraInfoMapper.selectCourseScoreDetail(stuNumber);
        if (courseScoreDetailVo != null){
            String enrollYear = "20" + stuNumber.substring(0,2);
            List<CourseScoreSemesterVo> courseScoreSemesterVoList = courseGradeMapper.selectCourseScoreSemesterList(stuNumber);
            // 学期课程信息
            List<CourseScoreSemesterVo> courseScoreSemesterList11 = new ArrayList<>();
            List<CourseScoreSemesterVo> courseScoreSemesterList12 = new ArrayList<>();
            List<CourseScoreSemesterVo> courseScoreSemesterList21 = new ArrayList<>();
            List<CourseScoreSemesterVo> courseScoreSemesterList22 = new ArrayList<>();
            List<CourseScoreSemesterVo> courseScoreSemesterList31 = new ArrayList<>();
            List<CourseScoreSemesterVo> courseScoreSemesterList32 = new ArrayList<>();
            List<CourseScoreSemesterVo> courseScoreSemesterList41 = new ArrayList<>();
            List<CourseScoreSemesterVo> courseScoreSemesterList42 = new ArrayList<>();
            for (CourseScoreSemesterVo courseScoreSemesterVo:courseScoreSemesterVoList) {
                Map<String,Integer> map = ExamSemesterCalculatorUtil.main(enrollYear,courseScoreSemesterVo.getExamDate());
                courseScoreSemesterVo.setExamDate("第"+map.get("academicYear")+"学年"+"第"+map.get("semester")+"学期");
                if ("第1学年第1学期".equals(courseScoreSemesterVo.getExamDate())){
                    courseScoreSemesterList11.add(courseScoreSemesterVo);
                }else if("第1学年第2学期".equals(courseScoreSemesterVo.getExamDate())){
                    courseScoreSemesterList12.add(courseScoreSemesterVo);
                }else if("第2学年第1学期".equals(courseScoreSemesterVo.getExamDate())){
                    courseScoreSemesterList21.add(courseScoreSemesterVo);
                }else if("第2学年第2学期".equals(courseScoreSemesterVo.getExamDate())){
                    courseScoreSemesterList22.add(courseScoreSemesterVo);
                }else if("第3学年第1学期".equals(courseScoreSemesterVo.getExamDate())){
                    courseScoreSemesterList31.add(courseScoreSemesterVo);
                }else if("第3学年第2学期".equals(courseScoreSemesterVo.getExamDate())){
                    courseScoreSemesterList32.add(courseScoreSemesterVo);
                }else if("第4学年第1学期".equals(courseScoreSemesterVo.getExamDate())){
                    courseScoreSemesterList41.add(courseScoreSemesterVo);
                }else if("第4学年第2学期".equals(courseScoreSemesterVo.getExamDate())){
                    courseScoreSemesterList42.add(courseScoreSemesterVo);
                }
            }
            courseScoreDetailVo.setCourseScoreSemesterList11(courseScoreSemesterList11);
            courseScoreDetailVo.setCourseScoreSemesterList12(courseScoreSemesterList12);
            courseScoreDetailVo.setCourseScoreSemesterList21(courseScoreSemesterList21);
            courseScoreDetailVo.setCourseScoreSemesterList22(courseScoreSemesterList22);
            courseScoreDetailVo.setCourseScoreSemesterList31(courseScoreSemesterList31);
            courseScoreDetailVo.setCourseScoreSemesterList32(courseScoreSemesterList32);
            courseScoreDetailVo.setCourseScoreSemesterList41(courseScoreSemesterList41);
            courseScoreDetailVo.setCourseScoreSemesterList42(courseScoreSemesterList42);
        }else {
            courseScoreDetailVo = new CourseScoreDetailVo();
        }
        return courseScoreDetailVo;
    }

    /**
    * @Description: 专业排名对比（辅导员）
    * @Param:
    * @return:
    * @Author: i++
    * @Date: 2023/7/12
    */
    public List<GroupedColumn> comparisonOfProfessionalRankingsForInstructor(String teacherNumber, String major){
        List<GroupedColumn> groupedColumnList = new ArrayList<>();
        //筛选班级
        List<String> classList = undergraGradeMapper.selectClassList(teacherNumber,major);
        if (classList.size() == 0)return new ArrayList<>();
        //查找班级群学生成绩信息
        List<GradeSummaryCourseGradesVo> gradeSummaryCourseGradesVoList = new ArrayList<>();
        List<GradeSummaryCourseGradesVo> gradeSummaryCourseGradesVoList1 = new ArrayList<>();
        if (classList.size() != 0){
            gradeSummaryCourseGradesVoList = undergraGradeMapper.selectCourseGradesInfoList(classList);
            gradeSummaryCourseGradesVoList1 = undergraGradeMapper.selectCourseGradesList(classList);
            for (GradeSummaryCourseGradesVo g:gradeSummaryCourseGradesVoList) {
                if (g.getStuNumber() == null)break;
                for (GradeSummaryCourseGradesVo g1:gradeSummaryCourseGradesVoList1) {
                    if (g.getStuNumber().equals(g1.getStuNumber())){
                        g.setCredit(g1.getCredit());
                        g.setGpa(g1.getGpa());
                        g.setClassRank(g1.getClassRank());
                        g.setMajorRank(g1.getMajorRank());
                        break;
                    }
                }
            }
        }else {
            return new ArrayList<>();
        }
        //横坐标是排名前%占比纵坐标是小数
        //计算并存入图表结构数据中
        for (String s: classList) {
            GroupedColumn groupedColumn10 = new GroupedColumn(s,"排名前10%占比",0);
            GroupedColumn groupedColumn30 = new GroupedColumn(s,"排名前30%占比",0);
            for (GradeSummaryCourseGradesVo g:gradeSummaryCourseGradesVoList) {
                if (s.equals(g.getClassName())){
                    if (g.getMajorRank() !=null && g.getMajorRank() < g.getStuCount()*0.3){
                        groupedColumn30.setYField(groupedColumn30.getYField()+1);
                    }
                    if (g.getMajorRank() !=null && g.getMajorRank() < g.getStuCount()*0.1){
                        groupedColumn10.setYField(groupedColumn10.getYField()+1);
                    }
                }
            }
            groupedColumnList.add(groupedColumn10);
            groupedColumnList.add(groupedColumn30);
        }
        return groupedColumnList;
    }

    /**
    * @Description: 专业排名对比（班导师）
    * @Param:
    * @return:
    * @Author: i++
    * @Date: 2023/7/12
    */
    public List<GroupedColumn> comparisonOfProfessionalRankingsForMentor(String teacherNumber){
        if (teacherNumber == null)return new ArrayList<>();
        List<GroupedColumn> groupedColumnList = new ArrayList<>();
        EducationTeamMemeber educationTeamMemeber = new EducationTeamMemeber();
        QueryWrapper<EducationTeamMemeber> educationTeamMemeberQueryWrapper = new QueryWrapper<>();
        educationTeamMemeberQueryWrapper.eq("instructor_number", teacherNumber);
        educationTeamMemeber = educationTeamMemeber.selectOne(educationTeamMemeberQueryWrapper);
        //筛选所在育人团队的班级
        List<String> classList = new ArrayList<>();
        List<String> classAndMentorList = new ArrayList<>();
        if (educationTeamMemeber != null && educationTeamMemeber.getLeaderNumber() != null){
            QueryWrapper<EducationTeamMemeber> educationTeamMemeberQueryWrapper1 = new QueryWrapper<>();
            educationTeamMemeberQueryWrapper1.eq("LEADER_NUMBER", educationTeamMemeber.getLeaderNumber());
            List<EducationTeamMemeber> educationTeamMemeberList = educationTeamMemeber.selectList(educationTeamMemeberQueryWrapper1);
            for (EducationTeamMemeber item: educationTeamMemeberList) {
                if(item.getClassName() != null){
                    classList.add(item.getClassName());
                    classAndMentorList.add(item.getClassName()+"("+item.getInstructor()+")");
                }
            }
        }
        //查找班级群学生成绩信息
        List<GradeSummaryCourseGradesVo> gradeSummaryCourseGradesVoList = new ArrayList<>();
        List<GradeSummaryCourseGradesVo> gradeSummaryCourseGradesVoList1 = new ArrayList<>();
        if (classList.size() != 0){
            gradeSummaryCourseGradesVoList = undergraGradeMapper.selectCourseGradesInfoList(classList);
            gradeSummaryCourseGradesVoList1 = undergraGradeMapper.selectCourseGradesList(classList);
            for (GradeSummaryCourseGradesVo g:gradeSummaryCourseGradesVoList) {
                if (g.getStuNumber() == null)break;
                for (GradeSummaryCourseGradesVo g1:gradeSummaryCourseGradesVoList1) {
                    if (g.getStuNumber().equals(g1.getStuNumber())){
                        g.setCredit(g1.getCredit());
                        g.setGpa(g1.getGpa());
                        g.setClassRank(g1.getClassRank());
                        g.setMajorRank(g1.getMajorRank());
                        break;
                    }
                }
            }
        }else {
            return new ArrayList<>();
        }
        //横坐标是排名前%占比纵坐标是小数
        //计算并存入图表结构数据中
        for (int i = 0; i < classList.size(); i++) {
            GroupedColumn groupedColumn10 = new GroupedColumn(classAndMentorList.get(i),"排名前10%占比",0);
            GroupedColumn groupedColumn30 = new GroupedColumn(classAndMentorList.get(i),"排名前30%占比",0);
            for (GradeSummaryCourseGradesVo g:gradeSummaryCourseGradesVoList) {
                if (classList.get(i).equals(g.getClassName())){
                    if (g.getMajorRank() !=null && g.getMajorRank() < g.getStuCount()*0.3){
                        groupedColumn30.setYField(groupedColumn30.getYField()+1);
                    }
                    if (g.getMajorRank() !=null && g.getMajorRank() < g.getStuCount()*0.1){
                        groupedColumn10.setYField(groupedColumn10.getYField()+1);
                    }
                }
            }
            groupedColumnList.add(groupedColumn10);
            groupedColumnList.add(groupedColumn30);
        }
        return groupedColumnList;
    }

    /**
    * @Description: 专业排名对比（育人团队负责人）
    * @Param:
    * @return:
    * @Author: i++
    * @Date: 2023/7/12
    */
    public List<GroupedColumn> comparisonOfProfessionalRankingsForMentorLeader(String teacherNumber){
        if (teacherNumber == null)return new ArrayList<>();
        List<GroupedColumn> groupedColumnList = new ArrayList<>();
        EducationTeamMemeber educationTeamMemeber = new EducationTeamMemeber();
        //筛选所在育人团队的班级
        List<String> classList = new ArrayList<>();
        List<String> classAndMentorList = new ArrayList<>();
        QueryWrapper<EducationTeamMemeber> educationTeamMemeberQueryWrapper = new QueryWrapper<>();
        educationTeamMemeberQueryWrapper.eq("LEADER_NUMBER", teacherNumber);
        List<EducationTeamMemeber> educationTeamMemeberList = educationTeamMemeber.selectList(educationTeamMemeberQueryWrapper);
        for (EducationTeamMemeber item: educationTeamMemeberList) {
            if(item.getClassName() != null){
                classList.add(item.getClassName());
                classAndMentorList.add(item.getClassName()+"("+item.getInstructor()+")");
            }
        }
        //查找班级群学生成绩信息
        List<GradeSummaryCourseGradesVo> gradeSummaryCourseGradesVoList = new ArrayList<>();
        List<GradeSummaryCourseGradesVo> gradeSummaryCourseGradesVoList1 = new ArrayList<>();
        if (classList.size() != 0){
            gradeSummaryCourseGradesVoList = undergraGradeMapper.selectCourseGradesInfoList(classList);
            gradeSummaryCourseGradesVoList1 = undergraGradeMapper.selectCourseGradesList(classList);
            for (GradeSummaryCourseGradesVo g:gradeSummaryCourseGradesVoList) {
                if (g.getStuNumber() == null)break;
                for (GradeSummaryCourseGradesVo g1:gradeSummaryCourseGradesVoList1) {
                    if (g.getStuNumber().equals(g1.getStuNumber())){
                        g.setCredit(g1.getCredit());
                        g.setGpa(g1.getGpa());
                        g.setClassRank(g1.getClassRank());
                        g.setMajorRank(g1.getMajorRank());
                        break;
                    }
                }
            }
        }else {
            return new ArrayList<>();
        }
        //横坐标是排名前%占比纵坐标是小数
        //计算并存入图表结构数据中
        for (int i = 0; i < classList.size(); i++) {
            GroupedColumn groupedColumn10 = new GroupedColumn(classAndMentorList.get(i),"排名前10%占比",0);
            GroupedColumn groupedColumn30 = new GroupedColumn(classAndMentorList.get(i),"排名前30%占比",0);
            for (GradeSummaryCourseGradesVo g:gradeSummaryCourseGradesVoList) {
                if (classList.get(i).equals(g.getClassName())){
                    if (g.getMajorRank() !=null && g.getMajorRank() < g.getStuCount()*0.3){
                        groupedColumn30.setYField(groupedColumn30.getYField()+1);
                    }
                    if (g.getMajorRank() !=null && g.getMajorRank() < g.getStuCount()*0.1){
                        groupedColumn10.setYField(groupedColumn10.getYField()+1);
                    }
                }
            }
            groupedColumnList.add(groupedColumn10);
            groupedColumnList.add(groupedColumn30);
        }
        return groupedColumnList;
    }

    /**
    * @Description: 学分绩点对比（辅导员）
    * @Param:
    * @return:
    * @Author: i++
    * @Date: 2023/7/12
    */
    public List<ClassScoreVo> gradeComparison(String teacherNumber, String major){
        List<GroupedColumn> groupedColumnList = new ArrayList<>();
        //筛选班级
        List<String> classList = undergraGradeMapper.selectClassList(teacherNumber,major);
        if (classList.size() == 0)return new ArrayList<>();
        List<ClassScoreVo> classScoreVoList = undergraGradeMapper.selectClassScoreList(classList);
        return classScoreVoList;
    }

    /**
    * @Description: 学分绩点对比（班导师）
    * @Param:
    * @return:
    * @Author: i++
    * @Date: 2023/7/12
    */
    public List<ClassScoreVo> gradeComparisonforMentor(String teacherNumber){
        if (teacherNumber == null)return new ArrayList<>();
        //筛选育人团队负责人
        EducationTeamMemeber educationTeamMemeber = new EducationTeamMemeber();
        QueryWrapper<EducationTeamMemeber> educationTeamMemeberQueryWrapper = new QueryWrapper<>();
        educationTeamMemeberQueryWrapper.eq("instructor_number", teacherNumber);
        educationTeamMemeber = educationTeamMemeber.selectOne(educationTeamMemeberQueryWrapper);
        //筛选所在育人团队的班级
        List<String> classList = new ArrayList<>();
        if (educationTeamMemeber != null && educationTeamMemeber.getLeaderNumber() != null){
            QueryWrapper<EducationTeamMemeber> educationTeamMemeberQueryWrapper1 = new QueryWrapper<>();
            educationTeamMemeberQueryWrapper1.eq("LEADER_NUMBER", educationTeamMemeber.getLeaderNumber());
            List<EducationTeamMemeber> educationTeamMemeberList = educationTeamMemeber.selectList(educationTeamMemeberQueryWrapper1);
            for (EducationTeamMemeber item: educationTeamMemeberList) {
                if(item.getClassName() != null){
                    classList.add(item.getClassName());
                }
            }
        }
        List<ClassScoreVo> classScoreVoList = undergraGradeMapper.selectClassScoreList(classList);
        return classScoreVoList;
    }

    /**
    * @Description: 学分绩点对比（育人团队）
    * @Param:
    * @return:
    * @Author: i++
    * @Date: 2023/7/12
    */
    public List<ClassScoreVo> gradeComparisonforLeader(String teacherNumber){
        if (teacherNumber == null)return new ArrayList<>();
        //筛选班级
        List<GroupedColumn> groupedColumnList = new ArrayList<>();
        EducationTeamMemeber educationTeamMemeber = new EducationTeamMemeber();
        //筛选所在育人团队的班级
        List<String> classList = new ArrayList<>();
        QueryWrapper<EducationTeamMemeber> educationTeamMemeberQueryWrapper = new QueryWrapper<>();
        educationTeamMemeberQueryWrapper.eq("LEADER_NUMBER", teacherNumber);
        List<EducationTeamMemeber> educationTeamMemeberList = educationTeamMemeber.selectList(educationTeamMemeberQueryWrapper);
        for (EducationTeamMemeber item: educationTeamMemeberList) {
            if(item.getClassName() != null){
                classList.add(item.getClassName());
            }
        }
        List<ClassScoreVo> classScoreVoList = undergraGradeMapper.selectClassScoreList(classList);
        return classScoreVoList;
    }
}





